<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Layouts {

    var $CI;
    var $content;
    var $title = NULL;
    var $title_separator = ' | ';
    var $data = array();
    var $js = array();
    var $css = array();
    var $layout_name = '';
    var $base_url;
    var $view_folder = '';

    /**
     * Constructor
     */
    public function __construct() {
        //Get an instance of CodeIgniter
        $this->CI = & get_instance();

        //Load variable from config file
        $this->CI->load->config('Layouts', TRUE); //2nd parameter TRUE - set Layouts as a variable to use
        //Get base url
        $this->CI->load->helper('url');
        $this->base_url = base_url();
    }

    /*
     * Set Title
     * Title we want to be displayed
     */

    public function set_title($name) {
        $this->title = $name;
    }

    /*
     * Set Javascript
     * Filename of the Javascript
     */

    public function set_js($name) {
        $this->js[] = $name;
    }

    /*
     * Set CSS 
     * File name of the CSS
     */

    public function set_css($name) {
        $this->css[] = $name;
    }

    /*
     * Set View Folder 
     * Filepath of the view folder contain content we want to display
     */

    public function set_view_folder($name) {
        $this->view_folder = $name . '/';
    }

    public function set_layout($name) {
        $this->layout_name = $name;
    }

    public function print_js() {

        //Merge autoload js with js set at controller
        $js_output_array = array_merge($this->CI->config->item('js_autoload', 'Layouts'), $this->js);

        //Output to view
        $js_folder = $this->CI->config->item('js_folder', 'Layouts'); //Access tf_assets key
        $js_suffix = $this->CI->config->item('js_suffix', 'Layouts');

        $js_output = '';
        foreach ($js_output_array as $key => &$value) {//Using & for reference instead of make a copy of variable
            $value = $this->base_url . $js_folder . $value . $js_suffix;
            $js_output .='<script type="text/javascript" src = "' . $value . '"></script>';
        }

        echo $js_output;
    }

    public function print_css() {

        //Merge autload css with css set at controller                
        $css_output_array = array_merge($this->CI->config->item('css_autoload', 'Layouts'), $this->css);

        //Output to view
        $css_folder = $this->CI->config->item('css_folder', 'Layouts');
        $css_suffix = $this->CI->config->item('css_suffix', 'Layouts');

        $css_output = '';
        foreach ($css_output_array as $key => &$value) {
            $value = $this->base_url . $css_folder . $value . $css_suffix;
            $css_output .= '<link rel="stylesheet" type="text/css" href="' . $value . '"/>';
        }

        echo $css_output;
    }

    public function get_image_path() {
        $img_folder = $this->CI->config->item('img_folder', 'Layouts');
        return $this->base_url . $img_folder;
    }

    public function load_image($image_name, $attributes = array(), $return = FALSE) {

        //Step 1: Get the image path from config
        $img_folder = $this->CI->config->item('img_folder', 'Layouts');
        $full_img_path = $this->base_url . $img_folder . $image_name;

        $img_output = '<img src="' . $full_img_path . '" ';

        //Step 2: If $attributes is not empty, append to <img>
        if (count($attributes) !== 0) {
            foreach ($attributes as $key => $value) {
                $img_output .= $key . ' = "' . $value . '" ';
            }
        }

        //Step 3: Close <img> tag and print output
        $img_output .= '/>';

        if ($return) {
            return $img_output;
        } else {
            echo $img_output;
        }
    }

    public function view($view_name, $data = array(), $layout='default') {

        //LOGIC: if user set layouts to be used, use that, else, $layout's name is default.php
        if ($this->layout_name !== '') {
            $layout = $this->layout_name;
        }

        /* LOGIC: If user use $this->layouts->set_view_folder() but later still put the folder path in load view, e.g. 'admin/content'
         * --> Skip using $this->layouts->view_folder()
         */

        $has_slash = strpos($view_name, '/');
        $view_name = ($has_slash !== FALSE ? $view_name : $this->view_folder . $view_name);

        //Render the content into a view and make it as a variable
        $rendered_view = $this->CI->load->view($view_name, $data, TRUE);

        if ($this->title !== NULL) {
            $this->title = $this->title_separator . $this->title;
        }

        //Pass the rendered content into the layout and output to a view
        $layout_folder = $this->CI->config->item('layout_folder', 'Layouts');

        $final_data = array(
            'title' => $this->title,
            'content' => $rendered_view
        );

        $this->CI->load->view($layout_folder . $layout, $final_data);
    }

    public function render_partial($partial_name, $data = array()) {
        $partial_folder = $this->CI->config->item('partial_folder', 'Layouts');
        $partial_path = $partial_folder . $partial_name;

        echo $this->CI->load->view($partial_path, $data, TRUE);
    }

}

// END Layouts class

/* End of file Layouts.php */
/* Location: ./application/libraries/Layouts.php */